<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :title="title"
    width="460px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="选择客服：" prop="copy_open_kfid">
        <el-select
          v-model="form.copy_open_kfid"
          placeholder="请选择客服"
          style="flex-grow: 1"
        >
          <el-option
            v-for="item in kfList"
            :key="item.open_kfid"
            :disabled="item.open_kfid == form.open_kfid ? true : false"
            :label="item.name"
            :value="item.open_kfid"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { setWeComCopyKf } from '@/api/enterpriseWeChat/weCom'
  export default defineComponent({
    name: 'TableEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        form: {
          open_kfid: '',
          corp_id: '',
          copy_open_kfid: '',
        },
        rules: {
          copy_open_kfid: [
            { required: true, trigger: 'blur', message: '请选择' },
          ],
        },
        title: '',
        kfList: [],
        dialogFormVisible: false,
      })
      const showEdit = (corp_id, open_kfid, kfList) => {
        state.title = '复制客服'
        state.kfList = kfList
        state.form.corp_id = corp_id
        state.form.open_kfid = open_kfid
        console.log(state.open_kfid)
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await setWeComCopyKf(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
